package com.wzx.ptplatform.controller.admin.company;

import com.wzx.ptplatform.domain.Company;
import com.wzx.ptplatform.domain.ReceivedResume;
import com.wzx.ptplatform.domain.Student;
import com.wzx.ptplatform.service.ReceivedResumeService;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员企业模块——收到兼职信息管理")
@RestController
public class AdminCompanyReceivedResumeController {

    final ReceivedResumeService receivedResumeService;

    public AdminCompanyReceivedResumeController(ReceivedResumeService receivedResumeService) {
        this.receivedResumeService = receivedResumeService;
    }

    @RequestMapping("admin_company_received_resume_message_list")
    public LayuiPager<ReceivedResume> adminCompanyReceivedResumeMessageList(
            String studentName, String companyName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        ReceivedResume receivedResume = new ReceivedResume();
        if (studentName != null && !"".equals(studentName)) {
            receivedResume.setStudent(new Student(studentName));
        }
        if (companyName != null && !"".equals(companyName)) {
            receivedResume.setCompany(new Company(companyName));
        }
        Page<ReceivedResume> pager = receivedResumeService.findList(receivedResume, page - 1, limit);
        LayuiPager<ReceivedResume> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_company_received_resume_message_editView")
    public ModelAndView adminCompanyReceivedResumeMessageEditView(String id, Model model) {
        model.addAttribute("receivedResume", receivedResumeService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/company/received_resume_message_edit");
    }

    @PostMapping("admin_company_received_resume_message_save_or_edit")
    public Map<String, Object> adminCompanyReceivedResumeSaveOrEdit(ReceivedResume receivedResume) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (receivedResumeService.saveOrEdit(receivedResume)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_company_received_resume_message_delete")
    public Map<String, Object> adminCompanyReceivedResumeMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (receivedResumeService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_company_received_resume_message_delete_select")
    public Map<String, Object> adminCompanyReceivedResumeMessageDeletedSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    receivedResumeService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
